/*
 * Copyright 2008-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ssn.login.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import egovframework.rte.fdl.string.EgovStringUtil;

import ssn.common.view.AjaxView;
import ssn.common.web.CommonController;
import ssn.login.service.LoginService;
import ssn.login.vo.LoginInfoVO;


/**  
 * @Class Name : 
 * @Description : 
 * @Modification Information  
 * @
 * @  수정일      수정자              수정내용
 * @ ---------   ---------   -------------------------------
 * @ 2013.10.09           최초생성
 * 
 * @since 2013. 09.30
 * @version 1.0
 * @see
 */
@SuppressWarnings("rawtypes")
@Controller
public class LoginController extends CommonController{
	
    @Resource(name = "loginService")
    private LoginService loginService;

    @RequestMapping(value="/login/loginIdCheck.do", method=RequestMethod.POST)
    public ModelAndView loginIdCheck(@RequestParam(value="chkId") String chkId) throws Exception {
    	ModelAndView mav = new ModelAndView(new AjaxView());

    	String result = "false";
    	if(loginService.selectCheckLoginId(chkId))
    		result = "true";
    	mav.addObject("result", result);
    	return mav;
    }
    
    @RequestMapping(value="/login/loginForm.do")
    public String loginForm(@ModelAttribute("loginVO") LoginInfoVO loginVO, ModelMap model) throws Exception {
    	model.addAttribute("loginVO", loginVO);
    	return "/login/loginForm";
    }
    
    
    @RequestMapping(value="/login/login.do", method=RequestMethod.GET)
    public String loginGet(@ModelAttribute("loginVO") LoginInfoVO loginVO, ModelMap model) throws Exception {
    	return "redirect:/login/loginForm.do";
    }
    
    @RequestMapping(value="/login/logout.do", method=RequestMethod.GET)
    public String loginout(HttpServletRequest request) throws Exception {
    	HttpSession session = request.getSession();
    	session.invalidate();
    	return "redirect:/login/loginForm.do";
    }
    
    @RequestMapping(value="/login/login.do", method=RequestMethod.POST)
    public String loginPost(@ModelAttribute("loginVO") @Valid LoginInfoVO loginVO, 
    		BindingResult bindingResult, RedirectAttributes redirectAttr) throws Exception {
    	if(bindingResult.hasErrors()){
    		for(FieldError error :  bindingResult.getFieldErrors()){
    			redirectAttr.addFlashAttribute("validErrorMsg", error.getDefaultMessage());
    		}
    		redirectAttr.addFlashAttribute("loginId", loginVO.getSsa_login_id());
    		redirectAttr.addFlashAttribute("loginVO", loginVO);
			return "redirect:/login/loginForm.do";
    	}
    	LoginInfoVO vo = loginService.selectLoginInfo(loginVO);
    	if(vo != null){
    		loginSessionInfo.get().setLoginInfo(vo); 
    	}else{
    		redirectAttr.addFlashAttribute("loginId", loginVO.getSsa_login_id());
    		redirectAttr.addFlashAttribute("validErrorMsg", "아이디 또는 패스워드가 틀렸습니다.");
    		redirectAttr.addFlashAttribute("loginVO", loginVO);
    		return "redirect:/login/loginForm.do";
    	}
    	if(EgovStringUtil.isNotEmpty(loginVO.getReferer())){
    		return "redirect:"+loginVO.getReferer();
    	}
    	return "redirect:/main.do";
    }
    
}
